package com.hezhiqin.initmqtt;

import com.alibaba.fastjson.JSON;
import com.hezhiqin.config.DynamicMqttClient;
import com.hezhiqin.config.MqttUtils;
import com.hezhiqin.config.NasInfo;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.ArrayList;

/**
 * @description:
 * @author: hezhiqin
 * @time: 2020/7/15 10:37
 */
@Component
public class  InItMqtt  implements ApplicationRunner {


    @Override
    public void run(ApplicationArguments args) throws Exception {
        ArrayList<NasInfo> nasInfos = new ArrayList<NasInfo>();
        NasInfo nasInfo =  new NasInfo();
        nasInfo.setClientId("75dcd3771ae84c29a5b4579daab2de82");
        nasInfo.setCompletionTimeout(3000);
        nasInfo.setDefaultTopic("/RealData/20064/#");
        nasInfo.setHostUrl("tcp://mqtt.zillion-ioe.com:1883");
        nasInfo.setUsername("user_1");
        nasInfo.setPassword("4118");


        NasInfo nasInfo2 =  new NasInfo();
        nasInfo2.setClientId("75dcd3771ae84c29a5b4579daab2de83");
        nasInfo2.setCompletionTimeout(3000);
        nasInfo2.setDefaultTopic("/RealData/20139/#");
        nasInfo2.setHostUrl("tcp://mqtt.zillion-ioe.com:1883");
        nasInfo2.setUsername("HBZHNY2020");
        nasInfo2.setPassword("JLYZC20200107");
        nasInfos.add(nasInfo);
        nasInfos.add(nasInfo2);

        System.out.println(JSON.toJSONString(nasInfos));

        for (NasInfo info: nasInfos) {
            DynamicMqttClient dynamicMqttClient = new DynamicMqttClient(info.getUsername(),info.getPassword(),info.getHostUrl(),info.getClientId(),info.getDefaultTopic(),info.getCompletionTimeout());
            dynamicMqttClient.connection();
            MqttClient mqttClient = dynamicMqttClient.getMqttClient();
            MqttUtils.concurrentHashMap.put(info.getClientId(),mqttClient);

        }



    }
}
